package kr.hkit.android_bookmgr.adapter;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import kr.hkit.android_bookmgr.R;
import kr.hkit.android_bookmgr.DTO.MemoData;
import kr.hkit.android_bookmgr.util.ImageUtil;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MemoBaseAdapter extends BaseAdapter { // 사용함 - 메모리스팅용
	private LayoutInflater mInflater;
	private ArrayList<MemoData> memoItems = new ArrayList<MemoData>();
	private Context context;
	private ContactViewHolder holder;

	public MemoBaseAdapter(Context context, ArrayList<MemoData> memoItems) {
		super();
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		this.memoItems = memoItems;
	}

	static class ContactViewHolder {

		TextView writedate;
		TextView wordspage;
		TextView review;
		TextView words;
		ImageView img;
		LinearLayout imgLinear;
	}

	@Override
	public int getCount() {
		return memoItems.size();
	}

	@Override
	public Object getItem(int position) {
		return memoItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.bookmemo_list_row, null);

			holder = new ContactViewHolder();

			holder.writedate = (TextView) convertView.findViewById(R.id.bookmemo_list_writedate);
			holder.wordspage = (TextView) convertView.findViewById(R.id.bookmemo_list_wordspage);
			holder.review = (TextView) convertView.findViewById(R.id.bookmemo_list_review);
			holder.words = (TextView) convertView.findViewById(R.id.bookmemo_list_words);
			holder.img = (ImageView) convertView.findViewById(R.id.bookmemo_list_memoimg);
			holder.imgLinear = (LinearLayout) convertView.findViewById(R.id.bookmemo_list_bottomlinear);
			
			convertView.setTag(holder);

		} else {
			holder = (ContactViewHolder) convertView.getTag();
		}

		MemoData item = memoItems.get(position);
		//Toast.makeText(context, item.getWritedate(), Toast.LENGTH_SHORT).show();

		if (item != null) {
			
			holder.writedate.setText("작성일: " + item.getWritedate().trim());
			holder.wordspage.setText(item.getWordspage() + " 페이지");
			holder.review.setText("서평: "+item.getReview());
			holder.words.setText(item.getWords());

			// 이미지
			ImageUtil.imageLoader(context);
			DisplayImageOptions options = ImageUtil.dispalyOptions();
			
			if (item.getMemoimgtype() == 0) { // 직찍
				ImageLoader.getInstance().displayImage("file:///" + item.getMemoimg(), holder.img, options);
			} else if(item.getMemoimgtype()==1) { // 갤러리
				ImageLoader.getInstance().displayImage("content://media/" + item.getMemoimg(), holder.img, options);
			} else if(item.getMemoimgtype()==-1) {
				holder.imgLinear.setVisibility(LinearLayout.GONE);
			}
		}
		return convertView;
	}// getView

	

}
